clear
clear matrix
program drop _all
capture log close
cd C:\Users\dpage\Desktop\

	set more off

use ESS1through8.dta, replace

tab cntry 

*Including Switzerland and Ukraine
replace RainbowIndex = 38.44 if country1==4
replace RainbowIndex = 20.95 if country1==31

set more off
			
	gen edu15 = EU15*eduyrs
	gen free15edu = EU15*freehms*eduyrs 
	*gen free15 = EU15*freehms
	gen freeedu = freehms*eduyrs
	gen lr15 = EU15*lrscale
	
	gen eduRB = RainbowIndex*eduyrs
	gen freeRBedu = RainbowIndex*freehms*eduyrs 
    gen freeRB = RainbowIndex*freehms
	gen lrRB = RainbowIndex*lrscale
	
	
	*********************************************
	*Table 3
	*********************************************
	
	#delimit ; 
	mixed Participation RainbowIndex freehms eduyrs 
	freeRB freeedu eduRB freeRBedu
	lrscale agea [pweight=myweight] ||countryyear:
	;
	#delimit cr
	
	#delimit ; 
	melogit vote RainbowIndex freehms eduyrs 
	freeRB freeedu eduRB freeRBedu
	lrscale agea [pweight=myweight] ||countryyear:
	;
	#delimit cr
	
set more off 
			 
		sum lrscale
			gen LR_mean = r(mean) 
		sum age 
			gen Age_mean = r(mean)
	
	set more off 
	
		keep if _n==1 
		keep LR_mean Age_mean 
		 
		matrix def b = e(b)
		matrix def V = e(V)
		mat list b
		mat list V
		
		set more off
		save FirstDifferencesIVs.dta, replace
		
		*program drop firstdiff2
	program define firstdiff2
	use FirstDifferencesIVs.dta, clear
	
	drawnorm b1-b11, means(b) cov(V)
		
    ***********************************
	*Feeling sexuality discrimination *
	***********************************
	
	
*Hi Education
	*Low Political Homophobia
		#delimit ;
		gen pParticipation15d = b1*67 + b2*5 + b3*16 +
		b4*67*5 + b5*5*16 + b6*67*16 + b7*67*5*16 + 
		b8*LR_mean + b9*Age_mean + b10
		;
		#delimit cr

		#delimit ;
		gen pParticipation15nd = b1*67 + b2*1 + b3*16 +
		b4*67*1 + b5*1*16 + b6*67*16 + b7*1*67*16 + 
		b8*LR_mean + b9*Age_mean + b10 
		;
		#delimit cr
	
	*Hi Political Homophobia
		#delimit ;
		gen pParticipationEd = b1*24 + b2*5 + b3*16 +
		b4*24*5 + b5*5*16 + b6*24*16 + b7*5*24*16 + 
		b8*LR_mean + b9*Age_mean + b10 
		;
		#delimit cr

		#delimit ;
		gen pParticipationEnd = b1*24 + b2*1 + b3*16 +
		b4*24*1 + b5*1*16 + b6*24*16 + b7*24*1*16 + 
		b8*LR_mean + b9*Age_mean + b10
		;
		#delimit cr
	

*Lo Education
	*Low Political Homophobia
		#delimit ;
		gen OnepParticipation15d = b1*67 + b2*5 + b3*8 +
		b4*67*5 + b5*8*5 + b6*67*8 + b7*67*5*8 + 
		b8*LR_mean + b9*Age_mean + b10
		;
		#delimit cr

		#delimit ;
		gen OnepParticipation15nd = b1*67 + b2*1 + b3*8 +
		b4*67*1 + b5*8*1 + b6*67*8 + b7*67*1*8 + 
		b8*LR_mean + b9*Age_mean + b10
		;
		#delimit cr
	
	*Hi Political Homophobia
		#delimit ;
		gen OnepParticipationEd = b1*24 + b2*5 + b3*8 +
		b4*24*5 + b5*5*8 + b6*24*8 + b7*5*24*8 + 
		b8*LR_mean + b9*Age_mean + b10
		;
		#delimit cr

		#delimit ;
		gen OnepParticipationEnd = b1*24 + b2*1 + b3*8 +
		b4*24*1 + b5*8*1 + b6*24*8 + b7*24*1*8 + 
		b8*LR_mean + b9*Age_mean + b10
		;
		#delimit cr

		
		gen Participation15d = exp(pParticipation15d) / (1+exp(pParticipation15d))
		gen Participation15nd = exp(pParticipation15nd) / (1+exp(pParticipation15nd))
		gen ParticipationEd = exp(pParticipationEd)/ (1+exp(pParticipationEd))
		gen ParticipationEnd = exp(pParticipationEnd)/ (1+exp(pParticipationEnd))
		
		gen OneParticipation15d = exp(OnepParticipation15d) / (1+exp(OnepParticipation15d))
		gen OneParticipation15nd = exp(OnepParticipation15nd) / (1+exp(OnepParticipation15nd))
		gen OneParticipationEd = exp(OnepParticipationEd) / (1+exp(OnepParticipationEd))
		gen OneParticipationEnd = exp(OnepParticipationEnd) / (1+exp(OnepParticipationEnd))
		
		
		gen DiffEast = ParticipationEd - ParticipationEnd
		gen DiffWest = Participation15d - Participation15nd
		
		gen OneDiffEast = OneParticipationEd - OneParticipationEnd
		gen OneDiffWest = OneParticipation15d - OneParticipation15nd

		append using FirstDiffs2.dta
			save FirstDiffs2.dta, replace
			
			end
							
			clear
			save FirstDiffs2.dta, replace emptyok
	
			set more off
			//simulating the creation of first differences at difference numbers of experiences
			simulate, reps(1500):  firstdiff2

			use FirstDiffs2.dta, clear

			
						set more off			
#delimit ; 
collapse (mean) 
Participation15d Participation15nd ParticipationEd ParticipationEnd DiffEast DiffWest
OneParticipation15d OneParticipation15nd OneParticipationEd OneParticipationEnd OneDiffEast OneDiffWest

 (sd)
 Participation15d_sd=Participation15d Participation15nd_sd=Participation15nd 
 ParticipationEd_sd=ParticipationEd ParticipationEnd_sd=ParticipationEnd
 DiffEast_sd=DiffEast  DiffWest_sd=DiffWest
 
 OneParticipation15d_sd=OneParticipation15d OneParticipation15nd_sd=OneParticipation15nd 
 OneParticipationEd_sd=OneParticipationEd OneParticipationEnd_sd=OneParticipationEnd
 OneDiffEast_sd=OneDiffEast  OneDiffWest_sd=OneDiffWest
 
 ;
#delimit cr 	


gen Participation15d_hi = Participation15d + 1.96*Participation15d_sd
gen Participation15d_lo = Participation15d - 1.96*Participation15d_sd

gen Participation15nd_hi = Participation15nd + 1.96*Participation15nd_sd
gen Participation15nd_lo = Participation15nd - 1.96*Participation15nd_sd

gen ParticipationEd_hi = ParticipationEd + 1.96*ParticipationEd_sd
gen ParticipationEd_lo = ParticipationEd - 1.96*ParticipationEd_sd

gen ParticipationEnd_hi = ParticipationEnd + 1.96*ParticipationEnd_sd
gen ParticipationEnd_lo = ParticipationEnd - 1.96*ParticipationEnd_sd 

gen DiffEast_hi = DiffEast + 1.96*DiffEast_sd
gen DiffEast_lo = DiffEast - 1.96*DiffEast_sd

gen DiffWest_hi = DiffWest + 1.96*DiffWest_sd
gen DiffWest_lo = DiffWest - 1.96*DiffWest_sd





gen OneParticipation15d_hi = OneParticipation15d + 1.96*OneParticipation15d_sd
gen OneParticipation15d_lo = OneParticipation15d - 1.96*OneParticipation15d_sd

gen OneParticipation15nd_hi = OneParticipation15nd + 1.96*OneParticipation15nd_sd
gen OneParticipation15nd_lo = OneParticipation15nd - 1.96*OneParticipation15nd_sd

gen OneParticipationEd_hi = OneParticipationEd + 1.96*OneParticipationEd_sd
gen OneParticipationEd_lo = OneParticipationEd - 1.96*OneParticipationEd_sd

gen OneParticipationEnd_hi = OneParticipationEnd + 1.96*OneParticipationEnd_sd
gen OneParticipationEnd_lo = OneParticipationEnd - 1.96*OneParticipationEnd_sd 

gen OneDiffEast_hi = OneDiffEast + 1.96*OneDiffEast_sd
gen OneDiffEast_lo = OneDiffEast - 1.96*OneDiffEast_sd

gen OneDiffWest_hi = OneDiffWest + 1.96*OneDiffWest_sd
gen OneDiffWest_lo = OneDiffWest - 1.96*OneDiffWest_sd

browse

gen axis_LFS1_B = 1
gen axis_LFS2_B = 2
gen axis_LFS3_B = 3
gen axis_LFS4_B = 4

lab def afmtlf2 1 "Homophobic" 2 "Tolerant" 3 "Homophobic" 4 "Tolerant" 
label val axis_LFS1_B afmtlf2 

#delimit ;
twoway (bar Participation15d axis_LFS1_B, 
			color(black) barwidth(.5))
		(rcap Participation15d_lo Participation15d_hi axis_LFS1_B,
			lcolor(gs10))
		(bar Participation15nd axis_LFS2_B, 
			color(black) barwidth(.5))
		(rcap Participation15nd_lo Participation15nd_hi axis_LFS2_B,
			lcolor(gs10))
		(bar ParticipationEd axis_LFS3_B , 
			color(gs10) barwidth(.5))
		(rcap ParticipationEd_lo ParticipationEd_hi axis_LFS3_B,
			lcolor(black))
		(bar ParticipationEnd axis_LFS4_B, 
			color(gs10) barwidth(.5))
		(rcap ParticipationEnd_lo ParticipationEnd_hi axis_LFS4_B,
			lcolor(black)),
legend(off)
ylabel(.4(.2)1)
ytitle("Pr(Voted)", size(medsmall))
title("High Education Level", size(medium))
xlabel(#3,valuelabel angle(horizontal))
name(GPH1, replace)
scheme(s1mono);
#delimit cr	


	    #delimit ;
		twoway (bar DiffWest axis_LFS1_B, color(black)) (rcap DiffWest_hi DiffWest_lo axis_LFS1_B, color(gs10)) 
		(bar DiffEast axis_LFS2_B, color(gs10)) (rcap DiffEast_hi DiffEast_lo axis_LFS2_B, color(black)), 
		scheme(s1mono) ylabel(.1(.1) -.1) ytitle("Difference in Pr(Voted)", size(medsmall))
		yline(0)
		xlabel( 1 "Low Political Homophobia" 2 "High Political Homophobia", noticks) 
		name(GPH2, replace)  
		 legend(off)
		;
		#delimit cr
		


		#delimit ;
		twoway (bar OneParticipation15d axis_LFS1_B, 
			color(black) barwidth(.5))
		(rcap OneParticipation15d_lo OneParticipation15d_hi axis_LFS1_B,
			lcolor(gs10))
		(bar OneParticipation15nd axis_LFS2_B, 
			color(black) barwidth(.5))
		(rcap OneParticipation15nd_lo OneParticipation15nd_hi axis_LFS2_B,
			lcolor(gs10))
		(bar OneParticipationEd axis_LFS3_B , 
			color(gs10) barwidth(.5))
		(rcap OneParticipationEd_lo OneParticipationEd_hi axis_LFS3_B,
			lcolor(black))
		(bar OneParticipationEnd axis_LFS4_B, 
			color(gs10) barwidth(.5))
		(rcap OneParticipationEnd_lo OneParticipationEnd_hi axis_LFS4_B,
			lcolor(black)),
legend(off)
ylabel(.4(.2) 1)
ytitle("Pr(Voted)", size(medsmall))
title("Low Education Level", size(medium))
xlabel(#3,valuelabel angle(horizontal))
name(GPH3, replace)
scheme(s1mono);
#delimit cr	


	    #delimit ;
		twoway (bar OneDiffWest axis_LFS1_B, color(black)) (rcap OneDiffWest_hi OneDiffWest_lo axis_LFS1_B, color(gs10)) 
		(bar OneDiffEast axis_LFS2_B, color(gs10)) (rcap OneDiffEast_hi OneDiffEast_lo axis_LFS2_B, color(black)), 
		scheme(s1mono) ylabel(.1(.1) -.1) ytitle("Difference in Pr(Voted)", size(medsmall))
		yline(0)
		xlabel( 1 "Low Political Homophobia" 2 "High Political Homophobia", noticks) 
		name(GPH4, replace)  
		 legend(off)
		;
		#delimit cr
			

#delimit ;	
	graph combine GPH1 GPH3 GPH2 GPH4, rows(2) title("Figure 1: Probability to vote by tolerance towards gay people, education level," 
" and level of political homophobia (high level 'grey' and low level 'black')" "with 95 percent confidence intervals", size(medsmall))
note("Dependent variable: Voted (1), Didn't vote(0)." "Data source: European Social Survey 2002-2016", size(medsmall)) scheme(s1mono)
;
#delimit cr

